using System;
using System.Collections.Generic;
using System.Linq;
using EntityFrameWork_Mysql_aspcore.Models;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MySql.Data.EntityFrameworkCore.Extensions;
namespace EntityFrameWork_Mysql_aspcore.Data {
    public class LibraryContext : DbContext {
        private IConfiguration _configration;

        public LibraryContext (DbContextOptions options) : base (options) {

        }
        public int Add (Book book) {
            using (this) {
                this.Book.Add (book);
                return this.SaveChanges ();
            }
        }
        public int Delete (string isbn) {
            using (this) {
                var bookFromContext = this.Book.FirstOrDefault (b => b.ISBN == isbn);
                this.Book.Remove (bookFromContext);
                return this.SaveChanges ();
            }
        }
        public int Update (Book book) {
            using (this) {
                var bookFromContext = this.Book.FirstOrDefault (b => b.ISBN == book.ISBN);
                bookFromContext.Title = book.Title;
                bookFromContext.Author = book.Author;
                bookFromContext.Language = book.Language;
                bookFromContext.Pages = book.Pages;
                bookFromContext.Publisher = book.Publisher;
                return this.SaveChanges ();
            }
        }
        public Book QueryByISBN (string isbn) {
            using (this) {
                return this.Book.FirstOrDefault (u => u.ISBN == isbn);
            }
        }
        public List<Book> QueryByAuthor (string author) {
            using (this) {
                return this.Book.Where (u => u.Author == author).ToList ();
            }
        }
        //分页查询
        public List<Book> QueryBookByPaging (int pageSize, int page) {
            using (this) {
                return this.Book.Skip (pageSize * (page - 1)).Take (pageSize).ToList ();
            }
        }
        /// <summary>
        /// 传入需要修改的目标语言，默认为English
        /// </summary>
        /// <param name="targetLanguage"></param>
        /// <returns></returns>
        public int QueryFixLanguage (string targetLanguage) {
            using (this) {
                using (var transaction = this.Database.BeginTransaction ()) {
                    try {
                        var bookListFromContext = this.Book.Where (b => b.Language == "English");
                        foreach (var item in bookListFromContext) {
                            item.Language = targetLanguage;
                        }
                        var count = this.SaveChanges ();
                        transaction.Commit ();
                        return count;
                    } catch (System.Exception) {
                        transaction.Rollback ();
                        return 0;
                    }
                }
            }
        }
        public DbSet<Book> Book { get; set; }
        public DbSet<Publisher> Publisher { get; set; }

    }
}